﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace JewererBasic.BO
{
    public enum TypeProcess
    {
        Waiting = 0, NonWaiting = 1, All = 2
    }

    public enum TableName
    {
        Type, Gamma, Unit, Kind
    }

    public class CommonServiceImp
    {        
        public IEnumerable KINDs()
        {
            MSQDBEntities dc = new MSQDBEntities();
            return dc.KINDs.ToList();
        }

        public IEnumerable TYPEs()
        {
            MSQDBEntities dc = new MSQDBEntities();
            return dc.TYPEs;
        }

        public IEnumerable GAMMAs()
        {
            MSQDBEntities dc = new MSQDBEntities();
            return dc.GAMMAs;
        }

        public IEnumerable UNITs()
        {
            MSQDBEntities dc = new MSQDBEntities();
            return dc.UNITs;
        }

        public IEnumerable PROCESSes(TypeProcess type)
        {
            MSQDBEntities dc = new MSQDBEntities();
            if (type == TypeProcess.All)
                return dc.PROCESSes;
            return dc.PROCESSes.Where(x => x.Type == (int)type);
        }


        public List<string> AutoCompleteMould(string query)
        {
            MSQDBEntities dc = new MSQDBEntities();
            return dc.MOULDs.Where(x => x.IdMould.Contains(query) == true).Select(x => x.IdMould).ToList();
        }


        public List<string> AutoCompleteStone(string query)
        {
            MSQDBEntities dc = new MSQDBEntities();
            return dc.STONEs.Where(x => x.Id.Contains(query) == true).Select(x => x.Id).ToList();
        }

        public List<string> AutoCompleteComponent(string query)
        {
            MSQDBEntities dc = new MSQDBEntities();
            return dc.COMPONENTs.Where(x => x.Id.Contains(query) == true).Select(x => x.Id).ToList();
        }        

        public List<string> AutoCompleteType(string query)
        {
            MSQDBEntities dc = new MSQDBEntities();
            return dc.TYPEs.Where(x => x.Name.Contains(query) == true).Select(x => x.Name).ToList();
        }

        public List<string> AutoCompleteUnit(string query)
        {
            MSQDBEntities dc = new MSQDBEntities();
            return dc.UNITs.Where(x => x.Name.Contains(query) == true).Select(x => x.Name).ToList();
        }

        public bool IsComponentExist(string componentId)
        {
            MSQDBEntities dc = new MSQDBEntities();
            COMPONENT com = dc.COMPONENTs.Find(componentId);
            return com == null ? false : true;
        }

        public bool IsStoneExist(string stoneId)
        {
            MSQDBEntities dc = new MSQDBEntities();
            var stone = dc.STONEs.Find(stoneId);
            return stone == null ? false : true;
        }

        public bool IsProductExist(string productId)
        {
            MSQDBEntities dc = new MSQDBEntities();
            var product = dc.PRODUCTs.Find(productId);
            return product == null ? false : true;
        }

        public bool IsMouldExist(string mouldId)
        {
            MSQDBEntities dc = new MSQDBEntities();
            var mould = dc.MOULDs.Find(mouldId);
            return mouldId == null ? false : true;
        }

        public bool IsProcessNameExist(string process)
        {
            MSQDBEntities dc = new MSQDBEntities();
            int count = dc.PROCESSes.Where(x => x.Name.Equals(process)).Count();
            if (count > 0)
                return true;
            return false;
        }

        public bool IsTypeNameExist(string name)
        {
            MSQDBEntities dc = new MSQDBEntities();
            var query = from d in dc.TYPEs
                        where d.Name.Equals(name)
                        select d;

            if (query.Count() > 0)
            {
                return true;
            }

            return false;
        }

        public bool IsUnitNameExist(string name)
        {
            MSQDBEntities dc = new MSQDBEntities();
            var query = from d in dc.UNITs
                        where d.Name.Equals(name)
                        select d;

            if (query.Count() > 0)
            {
                return true;
            }

            return false;
        }

        public int GetId(TableName table, string name)
        {
            MSQDBEntities dc = new MSQDBEntities();
            switch (table)
            {
                case TableName.Type:
                    return dc.TYPEs.Where(x => x.Name.Equals(name)).Select(x => x.Id).FirstOrDefault();
                case TableName.Gamma:
                    return dc.GAMMAs.Where(x => x.Name.Equals(name)).Select(x => x.Id).FirstOrDefault();
                case TableName.Unit:
                    return dc.UNITs.Where(x => x.Name.Equals(name)).Select(x => x.Id).FirstOrDefault();
                case TableName.Kind:
                    return dc.KINDs.Where(x => x.Name.Equals(name)).Select(x => x.Id).FirstOrDefault();
                default:
                    return 1;
            }
        }

        public List<EMPLOYEE> GetListEmployee()
        {
            MSQDBEntities db = new MSQDBEntities();
            return db.EMPLOYEEs.ToList();
        }

        public List<PROCESS> AutoCompleteProcess(string query, TypeProcess type)
        {
            MSQDBEntities dc = new MSQDBEntities();
            if (type == TypeProcess.All)
                return dc.PROCESSes.ToList();
            return dc.PROCESSes.Where(x => x.Type == (int)type).ToList();
        }
    }
}